<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <div>
          <el-button type="primary" round @click="addPermissionBtn(1,'0','add')">添加权限</el-button>
        </div>
        <!-- 表格 -->
        <!-- 当表格需要有树形结构时children,就要加上 row-key的属性去实施 row-key="id"的意思就是根据id 去找他是否有children 有显示小箭头,-->
        <el-table
          :data="tableData"
          border

          row-key="id"
          style="width: 100%"
        >
          <el-table-column
            fixed
            prop="name"
            label="名称"
            width="280"
          />
          <el-table-column
            prop="code"
            label="标识"
          />
          <el-table-column
            prop="description"
            label="描述"
          />

          <el-table-column
            fixed="right"
            label="操作"
          >
            <template slot-scope="scope">
              <!-- 因为看打印type===1是主 才可以增加 2是子不能加 -->
              <el-button v-if="scope.row.type===1" type="text" size="small" @click="addPermissionBtn(2, scope.row.id,'add')">增加</el-button>

              <el-button type="text" size="small" @click="addPermissionBtn(2, scope.row.id,'edadd')">编辑</el-button>
              <el-button v-if="scope.row.children.length===0" type="text" size="small" @click="daletedata(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 弹出框 -->
    <el-dialog
      :title="isEdit?'编辑':'新增'"
      :visible.sync="dialogVisible"
      width="50%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
      @close="cleardata"
    >
      <!-- 封装弹出框里面的内容,因为一般不会在这里写 ,但是今天这里也写一下吧-->
      <el-form ref="formRef" :model="form" label-width="80px">
        <el-form-item label="权限名称">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="form.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="form.enVisible"
            active-value="1"
            inactive-value="2"
            active-text="启用"
            inactive-text="不启用"
          >></el-switch>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">{{ isEdit?'更新':'新增' }}</el-button>
          <el-button @click="dialogVisible=false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { AdddataPermissions, adddataPermissions, DELETEdataPermissions, getdataPermissions, updataPermissions } from '@/api/permissions'
import { transformListToTree2 } from '@/utils/index'
export default {

  data() {
    return {
      tableData: [],
      dialogVisible: false,
      form: {
        enVisible: '',
        name: '',
        code: '',
        description: '',
        type: '',
        pid: ''

      },
      isEdit: false
    }
  },
  created() {
    this.Adddata()
  },
  methods: {
    handleClick(row) {
      console.log(row)
    },
    // 获取全部权限点
    async Adddata() {
      try {
        const res = await AdddataPermissions()
        if (res.success) {
          console.log('AdddataPermissions: ', res)
          // 前面pid ''是一级菜单 现在接口有变'0'是一级
          this.tableData = transformListToTree2(res.data, '0')
        } else {
          this.$message.error(res.message)
        }
      } catch (err) {
        console.log('权限接口有误', err)
      }
    },
    // 编辑获取表单数据请求
    async getdata(id) {
      try {
        const res = await getdataPermissions(id)
        if (res.success) {
          this.form = res.data
        } else {
          this.$message.error('编辑失败')
        }
        console.log('getdataPermissions: ', res)
      } catch (err) {
        console.log('getdataPermissions: ', err)
      }
    },
    // 编辑修改数据
    async updata() {
      try {
        const res = await updataPermissions(this.form)
        console.log('adddataPermissions: ', res)
        if (res.success) {
          this.$message.success('添加成功')
          this.dialogVisible = false
          this.Adddata()
        }
      } catch (err) {
        console.log('adddataPermissions: ', err)
      }
    },
    // 添加权限请求
    async adddata() {
      try {
        const res = await adddataPermissions(this.form)
        console.log('adddataPermissions: ', res)
        if (res.success) {
          this.$message.success('添加成功')
          this.dialogVisible = false
          this.Adddata()
        }
      } catch (err) {
        console.log('adddataPermissions: ', err)
      }
    },
    onSubmit() {
      // console.log(this.form.enVisible)
      if (this.isEdit) {
        this.updata()
      } else {
        this.adddata()
      }
    },
    // 区分编辑和增加
    addPermissionBtn(type, pid, isEditAdd) {
      if (isEditAdd === 'add') {
        this.isEdit = false
      } else {
        this.isEdit = true
        this.getdata(pid)
      }
      this.dialogVisible = true
      this.form.type = type
      this.form.pid = pid
    },
    // 删除请求
    async DELETEdata(id) {
      try {
        const res = await DELETEdataPermissions(id)
        if (res.success) {
          this.Adddata()
        }
        console.log('DELETEdataPermissions: ', res)
      } catch (err) {
        console.log('DELETEdataPermissions: ', err)
      }
    },
    // 删除按钮
    daletedata(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.DELETEdata(id)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 清空表单残留数据
    cleardata() {
      // 表单校验,设置初始值
      this.$refs.formRef.resetFields()
      // 这个是vue里面的自带初始化的对象
      this.form = this.$options.data().form
    }
  }
}
</script>
